package tacos.web;

import java.util.Optional;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import jakarta.servlet.http.HttpSession;
import jakarta.validation.Valid;
import org.springframework.validation.Errors;
import lombok.extern.slf4j.Slf4j;
import tacos.TacoOrder;

@Slf4j
@Controller
@RequestMapping("/orders")
public class OrderController {

    @GetMapping("/current")
    public String orderForm(Model model,HttpSession httpSession) {
        model.addAttribute("tacoOrder", new TacoOrder());
        log.info("跳转订单页面，是否有taco数据呢？{}",model.containsAttribute("taco"));
        log.info("session中是否有taco呢？{}", httpSession.getAttribute("taco"));
        return "orderForm";
    }

    @PostMapping
    public String processOrder(@Valid TacoOrder order,Errors errors,Model model) {
        if(errors.hasErrors()){
            log.info("提交订单后是否有taco数据{}",order.getTacos());
            log.info("model中的所有数据{}", model.asMap().toString());
            return "orderForm";
        }

        log.info("Order submitted: " + order);
        return "redirect:/";
    }

}